{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98a44163-8ca2-412b-8a46-36156502912b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Setup OpenAI Agent\n",
    "import os\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"sk-your-key\"\n",
    "\n",
    "from llama_index.core.agent.workflow import FunctionAgent\n",
    "from llama_index.llms.openai import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f61e4dde-928c-48f4-b9fa-bdb523574aa4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import and initialize our tool spec\n",
    "from llama_index.tools.gmail.base import GmailToolSpec\n",
    "\n",
    "tool_spec = GmailToolSpec()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8af5e53a-0d18-48cb-9f12-c2245da81edc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create the Agent with our tools\n",
    "agent = FunctionAgent(\n",
    "    tools=tool_spec.to_tool_list(),\n",
    "    llm=OpenAI(model=\"gpt-4.1\"),\n",
    ")\n",
    "\n",
    "# Context to store chat history\n",
    "from llama_index.core.workflow import Context\n",
    "ctx = Context(agent)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f038a10-0b35-40eb-a24d-88967e814be9",
   "metadata": {},
   "outputs": [],
   "source": [
    "await agent.run(\n",
    "    \"Can you create a new email to helpdesk and support @example.com about a service\"\n",
    "    \" outage\",\n",
    "    ctx=ctx,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82c25451-8327-4adc-a3da-a5f968133c94",
   "metadata": {},
   "outputs": [],
   "source": [
    "await agent.run(\"Update the draft so that it's the same but from 'Adam'\", ctx=ctx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e7137398-5540-44d8-b36b-7c89380c5205",
   "metadata": {},
   "outputs": [],
   "source": [
    "await agent.run(\"display the draft\", ctx=ctx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72eb181f-9c17-4496-a98b-8fd484d087a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "await agent.run(\"send the draft email\", ctx=ctx) "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
